package net.funwoo.pandago.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;

import net.funwoo.pandago.AppHelper;
import net.funwoo.pandago.R;
import net.funwoo.pandago.model.RegisterInfo;
import net.funwoo.pandago.server.UserService;
import net.funwoo.pandago.ui.BaseActivity;
import net.funwoo.pandago.util.HttpUtils;
import net.funwoo.pandago.util.StringUtils;
import net.funwoo.pandago.util.UiUtils;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText mUserIdText;
    private EditText mPasswdText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login, true);
    }

    @Override
    protected void initView() {
        mUserIdText = (EditText) findViewById(R.id.login_user_id);
        mPasswdText = (EditText) findViewById(R.id.login_passwd);

        TextView loginBtn = (TextView) findViewById(R.id.login_action_btn);
        TextView registerBtn = (TextView) findViewById(R.id.login_register_btn);
        TextView rememberBtn = (TextView) findViewById(R.id.login_remember);
        loginBtn.setOnClickListener(this);
        registerBtn.setOnClickListener(this);
        rememberBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.login_action_btn) {
            onLoginBtnClick();
        } else if (id == R.id.login_register_btn) {
            startActivity(new Intent(this, RegisterActivity.class));
        } else if (id == R.id.login_remember) {
            startActivity(new Intent(this, RememberActivity.class));
        }
    }

    private void onLoginBtnClick() {
        if (checkField()) {
            final MaterialDialog dialog = UiUtils.getLoadingDialog(this, getString(R.string.msg_login));
            dialog.show();

            RegisterInfo info = new RegisterInfo();
            info.email = mUserIdText.getText().toString();
            info.password = mPasswdText.getText().toString();

            UserService service = AppHelper.getRestAdapter().create(UserService.class);
            service.login(info, new Callback<Integer>() {
                @Override
                public void success(Integer result, Response response) {
                    if (result > 0) {
                        getAppInstance().setUserId(result);
                        LoginActivity.this.finish();
                    } else {
                        Toast.makeText(mContext, R.string.msg_no_user,
                                Toast.LENGTH_SHORT).show();
                        mUserIdText.requestFocus();
                    }

                    dialog.dismiss();
                }

                @Override
                public void failure(RetrofitError error) {
                    HttpUtils.onRequestFailed(mContext, error.getResponse());
                    dialog.dismiss();
                }
            });
        }
    }

    private boolean checkField() {
        String id = mUserIdText.getText().toString();
        String pwd = mPasswdText.getText().toString();

        // Empty check
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(pwd)) {
            Toast.makeText(this, R.string.msg_field_empty, Toast.LENGTH_SHORT).show();
            return false;
        }
//
//        // Length check
//        if (pwd.length() < 6) {
//            Toast.makeText(this, R.string.msg_password_less, Toast.LENGTH_SHORT).show();
//            return false;
//        }

//        // check id format
//        if (!StringUtils.isIdValid(id)) {
//            Toast.makeText(this, R.string.msg_userid_invalid, Toast.LENGTH_SHORT).show();
//            return false;
//        }

        return true;
    }


}
